// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Sprite)
    tyre: cc.Sprite = null;

    @property(cc.Label)
    score: cc.Label = null;

    @property(cc.Node)
    btn: cc.Node=null;

    @property
    status: number =0;

    @property
    contain=[];


    @property(cc.Prefab)
    arrow:cc.Prefab=null;
    onLoad () {

    }

    start () {
    }
    onClick(){
        if(this.status==0){
            this.status=1;
        }else if(this.status==1){
            let curdeg=this.tyre.node.angle;
            let cd=false
            if(this.hasContain(curdeg)){
                cd=true;
            }else{
                this.score.string=String(Number(this.score.string)-1);
            }
            let angle=curdeg/360;
            const arrow=cc.instantiate(this.arrow);
            const {x,y} =this.computeDeg(cd?200:160,curdeg);
            arrow.x=x;
            arrow.y=y;
            arrow.angle=90-curdeg;
            arrow.parent=this.tyre.node;
            if(cd){
                this.status=2;
            }
            if(this.score.string=="0"){
                console.log('过关')
                this.onClick();
            }
        }else if(this.status==2){
            this.tyre.node.removeAllChildren();
            this.contain=[];
            this.score.string="10"
            this.status=1;
        }
    }
    rotate(){
        if(this.tyre.node.angle<360){
            this.tyre.node.angle++;

        }else{
            this.tyre.node.angle-=360;
        }

    }
    hasContain(deg){
        let bool=false;

        this.contain.forEach(item=>{
            if(deg<item+10&&deg>item-10){
                bool= true
            }
        })
        if(!bool){
            this.contain.push(deg)
        }
        return bool;
    }
    update (dt) {
        if(this.status==1){
            this.rotate()
        }

    }
    computeDeg(radius,deg){
        let numpoints = 360;
        let phase = 2 * Math.PI/numpoints;

        let obj={
            y:-Math.round(radius * Math.cos(deg * phase)),
            x:-Math.round(radius * Math.sin(deg * phase)),
        }
        return obj;
    }
}
